package com.yy.controller.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.context.BaseContext;
import com.yy.controller.mapper.SetmealDishMapper;
import com.yy.controller.mapper.SetmealMapper;
import com.yy.controller.service.SetmealService;
import com.yy.dto.SetmealPageQueryDTO;
import com.yy.entity.Dish;
import com.yy.entity.Setmeal;
import com.yy.entity.SetmealDish;
import com.yy.exception.BaseException;
import com.yy.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     *条件分页查询套餐列表
     * @param setmealPageQueryDTO
     */
    @Override
    public PageResult pageList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page=setmealMapper.pageList(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除套餐
     * @param id
     */
    @Override
    public void delete(Long id) {
        //判断套餐是否处于起售中
        Setmeal setmeal = setmealMapper.setmealById(id);
        if(setmeal.getStatus()==1){
            throw new BaseException("套餐处于起售中，不能删除");
        }
        setmealMapper.delete(id);
    }

    /**
     * 新增套餐
     * @param setmeal
     */
    @Override
    @Transactional
    public void add(Setmeal setmeal) {
        //添加到套餐表
        Long userId = BaseContext.getCurrentId();
        setmeal.setCreateUser(userId);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateUser(userId);
        setmeal.setUpdateTime(LocalDateTime.now());
         setmealMapper.add(setmeal);
        //添加到菜品套餐关联表
        List<SetmealDish> setmealDishList=setmeal.getSetmealDishList();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.add(setmealDish);
        }
    }

    /**
     * 修改套餐
     * @param setmeal
     */
    @Override
    @Transactional
    public void update(Setmeal setmeal) {
        Long userId = BaseContext.getCurrentId();
        setmeal.setUpdateUser(userId);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.update(setmeal);
        //修该套餐关系表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishList();
        //删除该套餐关联的所有套餐菜品关联信息
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //添加新的套餐菜品关联信息

        for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setId(null);
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.add(setmealDish);
        }
    }
}
